package com.juku2024.juku.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.juku2024.juku.R
import com.juku2024.juku.tools.AnalysisUtils
import com.juku2024.juku.tools.AppUtils

class VideoDesDialog(context: Context, style: Int = R.style.Down2UpDialogStyle): Dialog(context, style) {

    private lateinit var btnBack: View
    private lateinit var topLayout: View
    private lateinit var tvVideoDes: TextView
    private lateinit var tvVideoUpdate: TextView
    private lateinit var tvVideoName: TextView
    private lateinit var tvVideoActor: TextView
    private lateinit var tvVideoDirector: TextView

    private var isInit = false
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.decorView?.setPadding(0, 0, 0, 0)
        val params = window?.attributes
        params?.width = ViewGroup.LayoutParams.MATCH_PARENT
        params?.height = ViewGroup.LayoutParams.MATCH_PARENT
        params?.gravity = Gravity.BOTTOM
        window?.attributes = params
        setContentView(R.layout.dialog_video_des)

        btnBack = findViewById(R.id.des_btnBack)
        topLayout = findViewById(R.id.video_des_tip)
        tvVideoDes = findViewById(R.id.des_tv_des)
        tvVideoActor = findViewById(R.id.des_tv_actor)
        tvVideoName = findViewById(R.id.des_tv_video_title)
        tvVideoUpdate = findViewById(R.id.des_tv_update)
        tvVideoDirector = findViewById(R.id.des_tv_director)

        btnBack.setOnClickListener {
            dismiss()
        }
        topLayout.setOnClickListener {
            dismiss()
        }

        isInit = true

        if (TextUtils.isEmpty(title)) return
        tvVideoName.text = title
        tvVideoDirector.text = getColorSpan(directorSpan, 3,directorSpan?.length?:0)
        tvVideoActor.text = getColorSpan(actorSpan, 3,actorSpan?.length?:0)
        tvVideoDes.text = getColorSpan(descriptionSpan, 3,descriptionSpan?.length?:0)
        if (updateSpan == "追剧日历：") {
            tvVideoUpdate.visibility = View.GONE
        } else {
            tvVideoUpdate.visibility = View.VISIBLE
            tvVideoUpdate.text = getColorSpan(updateSpan, 5,updateSpan?.length?:0)
        }
    }
    


    private var title: String? = null
    private var actorSpan: String? = null
    private var updateSpan: String? = null
    private var directorSpan: String? = null
    private var descriptionSpan: String? = null


    fun setDesInfo(title: String?, director: String?, actor: String?, description: String?, updateStr: String?) {
        this.title = title
        actorSpan = "演员：${actor?:""}"
        directorSpan = "导演：${director?:""}"
        descriptionSpan = "简介：${description?:""}"
        updateSpan = "追剧日历：${updateStr?:""}"


        AnalysisUtils.analysis("VideoDesEvent", "showDesTimes")

        if (!isInit) return
        setUI(title, updateStr)
    }

    private fun setUI(title: String?, updateStr: String?) {
        tvVideoName.text = title
        tvVideoDirector.text = getColorSpan(directorSpan, 3, directorSpan?.length ?: 0)
        tvVideoActor.text = getColorSpan(actorSpan, 3, actorSpan?.length ?: 0)
        tvVideoDes.text = getColorSpan(descriptionSpan, 3, descriptionSpan?.length ?: 0)

        if (TextUtils.isEmpty(updateStr)) {
            tvVideoUpdate.visibility = View.GONE
        } else {
            tvVideoUpdate.visibility = View.VISIBLE
            tvVideoUpdate.text = getColorSpan(updateSpan, 5, updateSpan?.length ?: 0)
        }
    }

    private fun getColorSpan(des: String?, start: Int, end: Int): SpannableString? {
        if (TextUtils.isEmpty(des)) return null
        val spanString = SpannableString(des)
        if (start > end) {
            return spanString
        }
        val span = ForegroundColorSpan(AppUtils.getColor(R.color.c9C9C9C))
        spanString.setSpan(span, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        return spanString
    }


}